#!/usr/bin/python
import json,sys
from contextlib import contextmanager
import MySQLdb
def __virtual__():
    return 'mysql_return'
def returner(ret):
    '''
    Return data to the local file
    '''
#    result_file = '/var/local/salt/returner'
#    result = file(result_file,'a+')
#    result.write(str(json.dumps(ret['fun']))+'\n')
#    result.write(str(json.dumps(ret['jid']))+'\n')
#    result.write(str(json.dumps(ret['return']))+'\n')
#    result.write(str(json.dumps(ret['id']))+'\n')
#    result.write(str(json.dumps(ret['success']))+'\n')
#    result.write(str(json.dumps(ret))+'\n')
#    result.close()
    conn=MySQLdb.connect(host='salt',user='salt',passwd='salt',db='salt',port=3306)
    cursor=conn.cursor()
    cursor.execute('INSERT INTO salt_returns VALUES (%s,%s,%s,%s,%s,%s)' % (str(json.dumps(ret['fun'])),str(json.dumps(ret['jid'])),"'"+str(json.dumps(ret['return']))+"'",str(json.dumps(ret['id'])),'"'+str(ret['success'])+'"',"'"+json.dumps(ret)+"'"))
    conn.commit()
    cursor.close()
    conn.close()

